/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaDomain;
import han.bifang.common.entity.SoaServiceCatalog;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class ServiceCatalogDao {
	private static Log logger = LogFactory.getLog(ServiceCatalogDao.class);

	public static List getServiceCatalogByDomainId(String domainId)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String sql = "from SoaServiceCatalog s where s.domainId = ? ";
			List cat = DaoManager.getDao().find(sql, domainId);

			tx.commit();

			return cat;

		} catch (Throwable e) {
			logger.error("ServiceCatalogDao.getServiceCatalogByDomainId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceCatalogByDomainId() error", e);
		} finally {
			tx.end();
		}

	}

	public static void createServiceCatalog(SoaServiceCatalog cat)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaServiceCatalog s where s.code = ? and s.domainId = ?";
			Object[] o = new Object[] { cat.getCode(), cat.getDomainId() };
			List cl = DaoManager.getDao().find(hql, o);
			if (cl.size() > 0) {
				throw new BifangException(
						BifangErrorCode.CATALOG_CODE_EXISTS_ERROR,
						"ServiceCatalog code " + cat.getCode()
								+ " exists in this domain.");

			}
			DaoManager.getDao().save(cat);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (BifangException e) {
			throw e;
		} catch (Throwable e) {
			logger.error("ServiceCatalog.createServiceCatalog()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createServiceCatalog() error", e);
		} finally {
			tx.end();
		}

	}

	public static void modifyServiceCatalog(SoaServiceCatalog cat)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaServiceCatalog s where s.code = ? ";
			List l = DaoManager.getDao().find(hql, cat.getCode());
			if (l.size() > 0) {
				SoaServiceCatalog t = (SoaServiceCatalog) l.get(0);
				if (!t.getId().equals(cat.getId()))
					throw new BifangException(
							BifangErrorCode.CATALOG_CODE_EXISTS_ERROR,
							"SerivceCatalog code " + cat.getCode() + " exists.");

			}
			DaoManager.getDao().merge(cat);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			logger.error("ServiceCatalog.modifyServiceCatalog()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"modifyServiceCatalog() error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeServiceCatalog(SoaServiceCatalog cat)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			DaoManager.getDao().delete(cat);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			logger.error("ServiceCatalog.removeServiceCatalog()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeServiceCatalog() error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeServiceCatalog(String catId)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaServiceCatalog cat = (SoaServiceCatalog) DaoManager.getDao()
					.get(SoaServiceCatalog.class, catId);
			if (cat == null)
				throw new BifangException(
						BifangErrorCode.RECORD_NOT_EXISTS_ERROR,
						"SoaServiceCatalog[id=" + catId + "] doesn't exist.");
			DaoManager.getDao().delete(cat);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			logger.error("ServiceCatalog.removeServiceCatalog(String)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeServiceCatalog(String) error", e);
		} finally {
			tx.end();
		}

	}

	public static SoaServiceCatalog getServiceCatalog(String catId)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaServiceCatalog cat = (SoaServiceCatalog) DaoManager.getDao()
					.get(SoaServiceCatalog.class, catId);

			tx.commit();
			return cat;

		} catch (Throwable e) {
			logger.error("ServiceCatalog.getServiceCatalog()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceCatalog() error", e);
		} finally {
			tx.end();
		}

	}

	public static SoaServiceCatalog getDefaultServiceCatalog()
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			
			String hql = "from SoaDomain s where s.local = '1'";
			List<SoaDomain> dl = DaoManager.getDao().find(hql);
			
			String domainId = dl.get(0).getId();
			
			hql = "from SoaServiceCatalog s where s.domainId = ? ";
			
			List<SoaServiceCatalog> cl = DaoManager.getDao().find(hql, domainId);
			SoaServiceCatalog cat = cl.get(0);

			tx.commit();
			return cat;

		} catch (Throwable e) {
			logger.error("ServiceCatalog.getServiceCatalog()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceCatalog() error", e);
		} finally {
			tx.end();
		}

	}
}
